package org.gaojianqiao.electronichandbook.database;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import org.gaojianqiao.electronichandbook.createtable.UserLoginRecordTab;
import org.gaojianqiao.electronichandbook.createtable.user;

import zyg.login.DataBas;

public class OperateUserLoginRecordTab
{
	public ArrayList<UserLoginRecordTab> UserLoginRecordlist(String bookName)
			throws SQLException, UnsupportedEncodingException
	{

		// TODO Auto-generated method stub

		String sql = "select * from " + bookName;
		DataBas publicDataBase = new DataBas();
		publicDataBase.connectDB("data3");
		ResultSet rs = publicDataBase.stmt.executeQuery(sql);
		ArrayList<UserLoginRecordTab> UserLoginRecordlist = new ArrayList<UserLoginRecordTab>();
		while (rs.next())
		{
			UserLoginRecordTab UserLoginRecord = new UserLoginRecordTab();
			UserLoginRecord.setid(rs.getString("id"));
			UserLoginRecord.setusername(rs.getString("username"));
			UserLoginRecord.setdata(rs.getString("data"));
			// System.out.println(""+rs.getString("weapon_id")+rs.getString("weapon_title")+rs.getString("UserLoginRecord"));
		}
		publicDataBase.closeDB();

		return UserLoginRecordlist;
	}

	public void insert(ArrayList<UserLoginRecordTab> str) throws SQLException,
			ClassNotFoundException
	{
		String sql = "insert into UserLoginRecordTab values (?,?)";
		DataBas publicDataBase = new DataBas();
		Connection conn = publicDataBase.connectDB("public");
		PreparedStatement ps = conn.prepareStatement(sql);
		int i = 0;
		do
		{
			ps.setString(1, str.get(i).getid());
			ps.setString(2, str.get(i).getusername());
			ps.setString(3, str.get(i).getdata());
			ps.executeUpdate();
			++i;
		} while (i < str.size());
		ps.close();
		conn.close();

	}

	public void createtable( ) throws SQLException, ClassNotFoundException
	{

		String sql2 = "drop table if exists UserLoginRecordTab";
		String sql3 = "create table UserLoginRecordTab (id text,username text,data text)";
		DataBas publicDataBase = new DataBas();
		Connection conn = publicDataBase.connectDB("data3");
		Statement stat = conn.createStatement();
		conn.setAutoCommit(false);
		stat.executeUpdate(sql2);
		stat.executeUpdate(sql3);
		conn.commit();
		conn.close();
	}

}
